// Author: Civic TSI

num hmap00;
num hmap01;
num hmap02;
num hmap03;
num hmap04;
num hmap05;
num hmap06;
num hmap07;
num hmap08;
num hmap09;
num hmap10;
num hmap11;
num hmap12;
num hmap13;
num hmap14;

num hmapscalarmode;

void open()
{
	num x = 10;
	num y = 275;
	CreateBorder(x, y, 790, 90, 6, 0, "");
	CreateStatic(x+5, y-5, 80, 20, 6, 0, "Map Scalar");

	hmap00 = CreateEdit(x+20, y+20, 50, 20, 6, 0, "");
	hmap01 = CreateEdit(x+70, y+20, 50, 20, 6, 0, "");
	hmap02 = CreateEdit(x+120, y+20, 50, 20, 6, 0, "");
	hmap03 = CreateEdit(x+170, y+20, 50, 20, 6, 0, "");
	hmap04 = CreateEdit(x+220, y+20, 50, 20, 6, 0, "");
	hmap05 = CreateEdit(x+270, y+20, 50, 20, 6, 0, "");
	hmap06 = CreateEdit(x+320, y+20, 50, 20, 6, 0, "");
	hmap07 = CreateEdit(x+370, y+20, 50, 20, 6, 0, "");
	hmap08 = CreateEdit(x+420, y+20, 50, 20, 6, 0, "");
	hmap09 = CreateEdit(x+470, y+20, 50, 20, 6, 0, "");
	hmap10 = CreateEdit(x+520, y+20, 50, 20, 6, 0, "");
	hmap11 = CreateEdit(x+570, y+20, 50, 20, 6, 0, "");
	hmap12 = CreateEdit(x+620, y+20, 50, 20, 6, 0, "");
	hmap13 = CreateEdit(x+670, y+20, 50, 20, 6, 0, "");
	hmap14 = CreateEdit(x+720, y+20, 50, 20, 6, 0, "");
	ReadMapScalar();

	y = y + 50;
	CreateBtn(x+20, y, 50, 20, 6, "ReadMapScalar", "Read");
	CreateBtn(x+320, y, 50, 20, 6, "ShiftMapScalarLeft", "<<");
	CreateBtn(x+420, y, 50, 20, 6, "ShiftMapScalarRight", ">>");
	CreateBtn(x+720, y, 50, 20, 6, "WriteMapScalar", "Write");
//	CreateBtn(x+70, y, 50, 20, 6, "ToggleMapScalar", "Toggle");

	hmapscalarmode = 0;
}

void ReadMapScalar()
{
	setitem(hmap00, MBar2InHgPsi(MBAR_TABLE, 0));
	setitem(hmap01, MBar2InHgPsi(MBAR_TABLE, 1));
	setitem(hmap02, MBar2InHgPsi(MBAR_TABLE, 2));
	setitem(hmap03, MBar2InHgPsi(MBAR_TABLE, 3));
	setitem(hmap04, MBar2InHgPsi(MBAR_TABLE, 4));
	setitem(hmap05, MBar2InHgPsi(MBAR_TABLE, 5));
	setitem(hmap06, MBar2InHgPsi(MBAR_TABLE, 6));
	setitem(hmap07, MBar2InHgPsi(MBAR_TABLE, 7));
	setitem(hmap08, MBar2InHgPsi(MBAR_TABLE, 8));
	setitem(hmap09, MBar2InHgPsi(MBAR_TABLE, 9));
	setitem(hmap10, MBar2InHgPsi(MBAR_TABLE, 10));
	setitem(hmap11, MBar2InHgPsi(MBAR_TABLE, 11));
	setitem(hmap12, MBar2InHgPsi(MBAR_TABLE, 12));
	setitem(hmap13, MBar2InHgPsi(MBAR_TABLE, 13));
	setitem(hmap14, MBar2InHgPsi(MBAR_TABLE, 14));
}

void MBar2InHgPsi(num src_map, num offset)
{
	num mbar;
	num val;

	src_map = src_map + offset;
	mbar = readbyte(src_map);
	if((offset == 14) & (mbar == 0))
	{
		mbar = 255;
	}
	else
	{
		mbar = mbar + 24;
		if(mbar > 254)
			mbar = 254;
	}

	mbar = ((mbar / 255) * 1000);
	val = ((((mbar * (readfloat(MAPSENSOR_FLOAT)))-1000)/1000)*14.7);
	if(val < 0)
		val = ((((mbar * (readfloat(MAPSENSOR_FLOAT)))-1000)/1000)*29.54);

	return round(val,2);
}

void WriteMapScalar()
{
	if(hmapscalarmode == 1)
	{
		ToggleMapScalar();
	}

	writebyte(MBAR_TABLE + 0, InHgPsi2MBar(getitem(hmap00),0));
	writebyte(MBAR_TABLE + 1, InHgPsi2MBar(getitem(hmap01),1));
	writebyte(MBAR_TABLE + 2, InHgPsi2MBar(getitem(hmap02),2));
	writebyte(MBAR_TABLE + 3, InHgPsi2MBar(getitem(hmap03),3));
	writebyte(MBAR_TABLE + 4, InHgPsi2MBar(getitem(hmap04),4));
	writebyte(MBAR_TABLE + 5, InHgPsi2MBar(getitem(hmap05),5));
	writebyte(MBAR_TABLE + 6, InHgPsi2MBar(getitem(hmap06),6));
	writebyte(MBAR_TABLE + 7, InHgPsi2MBar(getitem(hmap07),7));
	writebyte(MBAR_TABLE + 8, InHgPsi2MBar(getitem(hmap08),8));
	writebyte(MBAR_TABLE + 9, InHgPsi2MBar(getitem(hmap09),9));
	writebyte(MBAR_TABLE + 10, InHgPsi2MBar(getitem(hmap10),10));
	writebyte(MBAR_TABLE + 11, InHgPsi2MBar(getitem(hmap11),11));
	writebyte(MBAR_TABLE + 12, InHgPsi2MBar(getitem(hmap12),12));
	writebyte(MBAR_TABLE + 13, InHgPsi2MBar(getitem(hmap13),13));
	writebyte(MBAR_TABLE + 14, InHgPsi2MBar(getitem(hmap14),14));

	ReadMapScalar();
}

void InHgPsi2MBar(num val, num offset)
{
	num mbar;

	if(val < 0)
	{
		mbar = (((((((val)/29.54)*1000)+1000)/1000)/(readfloat(MAPSENSOR_FLOAT)))*255);
	}
	else
	{
		mbar = (((((((val)/14.7)*1000)+1000)/1000)/(readfloat(MAPSENSOR_FLOAT)))*255);
	}

	if((offset == 14) & (mbar >= 255))
	{
		mbar = 0;
	}
	else 
	{
		mbar = mbar - 24;
		if(mbar < 0)
			mbar = 0;
	}

	return round(mbar,0);
}

void ShiftMapScalarLeft()
{
	ShiftMapLeft(MBAR_TABLE, NEW_TABLELO_X, 1);
	ShiftMapLeft(IGN_BOOST_TABLE, NEW_TABLELO_X, NEW_TABLELO_Y);
	ShiftMapLeft(IGNHI_BOOST_TABLE, NEW_TABLEHI_X, NEW_TABLEHI_Y);
	ShiftMapLeft(FUEL_BOOST_TABLE, NEW_TABLELO_X, NEW_TABLELO_Y);
	ShiftMapLeft(FUELHI_BOOST_TABLE, NEW_TABLEHI_X, NEW_TABLEHI_Y);
	ReadMapScalar();
	PopulateTables();
}

void ShiftMapLeft(num src_map, num dx, num dy)
{
	num x = 0;
	while( x < dx-1)
	{
		num y = 0;
		while(y < dy)
		{
			num src_byte = readbyte(src_map+(y*dx)+x+1);
			num dest_addr = src_map+(y*dx)+x;
			writebyte(dest_addr, src_byte);
			y = y + 1;
		}
		x = x + 1;
	}
}

void ShiftMapScalarRight()
{
	ShiftMapRight(MBAR_TABLE, NEW_TABLELO_X, 1);
	ShiftMapRight(IGN_BOOST_TABLE, NEW_TABLELO_X, NEW_TABLELO_Y);
	ShiftMapRight(IGNHI_BOOST_TABLE, NEW_TABLEHI_X, NEW_TABLEHI_Y);
	ShiftMapRight(FUEL_BOOST_TABLE, NEW_TABLELO_X, NEW_TABLELO_Y);
	ShiftMapRight(FUELHI_BOOST_TABLE, NEW_TABLEHI_X, NEW_TABLEHI_Y);
	ReadMapScalar();
	PopulateTables();
}

void ShiftMapRight(num src_map, num dx, num dy)
{
	num x = dx - 1;
	while( x > 0)
	{
		num y = 0;
		while(y < dy)
		{
			num src_byte = readbyte(src_map+(y*dx)+x-1);
			num dest_addr = src_map+(y*dx)+x;
			writebyte(dest_addr, src_byte);
			y = y + 1;
		}
		x = x - 1;
	}
}


void ToggleMapScalar()
{
	if(hmapscalarmode == 0)
	{
		hmapscalarmode = 1;
		ReadDirectMapScalar();
	}
	else
	{
		hmapscalarmode = 0;
		ReadMapScalar();
	}
}

void ReadDirectMapScalar()
{
	setitem(hmap00, readbyte(MBAR_TABLE + 0));
	setitem(hmap01, readbyte(MBAR_TABLE + 1));
	setitem(hmap02, readbyte(MBAR_TABLE + 2));
	setitem(hmap03, readbyte(MBAR_TABLE + 3));
	setitem(hmap04, readbyte(MBAR_TABLE + 4));
	setitem(hmap05, readbyte(MBAR_TABLE + 5));
	setitem(hmap06, readbyte(MBAR_TABLE + 6));
	setitem(hmap07, readbyte(MBAR_TABLE + 7));
	setitem(hmap08, readbyte(MBAR_TABLE + 8));
	setitem(hmap09, readbyte(MBAR_TABLE + 9));
	setitem(hmap10, readbyte(MBAR_TABLE + 10));
	setitem(hmap11, readbyte(MBAR_TABLE + 11));
	setitem(hmap12, readbyte(MBAR_TABLE + 12));
	setitem(hmap13, readbyte(MBAR_TABLE + 13));
	setitem(hmap14, readbyte(MBAR_TABLE + 14));
}
